package cn.chencq.spider.novel.service.chapterdetail.impl;

import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import cn.chencq.spider.novel.service.AbstractSpider;
import cn.chencq.spider.novel.service.chapterdetail.IChapterDetailSpider;
import cn.chencq.spider.novel.utils.SpiderRuleUtils;
import cn.chencq.spider.novel.utils.Utils;
import cn.chencq.spider.novel.vo.Chapter;
import cn.chencq.spider.novel.vo.ChapterDetail;
import cn.chencq.spider.novel.vo.Selector;

public class ChapterDetailSpider extends AbstractSpider implements IChapterDetailSpider {

	@Override
	public ChapterDetail getChapterDetail(Chapter chapter) {
		ChapterDetail detail = null;
		String url = chapter.getChapterUrl();
		try {
			Document doc = super.crawl(url);
			String domain = Utils.getDomain(url);// 获取链接的主站域名
			Selector selector = SpiderRuleUtils.getSpiderSelector(domain);
			Elements els = doc.select(selector.getChapterDetailContentSelector());
			if (null != els) {
				String content = els.first().html();
				detail = new ChapterDetail();
				detail.setContent(content);
				detail.setChapterName(chapter.getChapterName());
			}
		} catch (Exception e) {
			detail = null;
			e.printStackTrace();
		}
		return detail;
	}

}
